package org.alex.samples.dao;

import java.util.ArrayList;
import java.util.List;

import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ShowExamScore;
import org.alex.samples.model.Sub;
import org.hibernate.transform.Transformers;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class ExamScoreImpl extends BaseHibernateDAO<ExamScore> implements IExamScore {
	
	@Override
	public List<ExamScore> getAllScoreBysubId(int subId){
		String hql ="from ExamScore es Where  es.subId ="+subId;
		List<ExamScore> list = this.find(hql);
		return list;
	}

	@Override
	public ExamScoreVO getScore(String userId, int subId) {

	    
	    String hql ="from ExamScore es Where es.userId =:userId and es.subId =:subId";
	    ExamScore examScore =  (ExamScore) this.createQuery(hql).setString("userId", userId).setInteger("subId", subId).uniqueResult();
	    ExamScoreVO vo = new ExamScoreVO();
	    if(examScore !=null){
	    BeanUtils.copyProperties(examScore, vo);
	    }
	    return vo;
	    
//		String hql="from ExamScore";
//		List<ExamScore> scoreList = this.find(hql);
//		List<ExamScoreVO> resultList = new ArrayList<ExamScoreVO>();
//		for(ExamScore subject : scoreList){
//			ExamScoreVO vo = new ExamScoreVO();
//			BeanUtils.copyProperties(subject, vo);
//			resultList.add(vo);
//		}
//		for(ExamScoreVO list:resultList){
//			if(list.getUserId().equals(userId)  && list.getSubId()==subId){
//				return list;
//			}
//		}
//	    return null;
	}

	@Override
	public ExamScoreVO getScoreById(Integer scoreId) {
		StringBuilder hql = new StringBuilder();
		hql.append(" select user.id as userId,sub.name as subjectName,es.examTime as examTime,es.score as score");
		hql.append(" from ExamScore es,User user,Sub sub");
		hql.append(" where es.userId = user.id");
		hql.append(" and es.subId = sub.id ");
		hql.append(" and es.id = :scoreId ");

		return (ExamScoreVO) this.createQuery(hql.toString())
				.setResultTransformer(Transformers.aliasToBean(ExamScoreVO.class))
				.setInteger("scoreId", scoreId).uniqueResult();
	}
	@Override
	public List<ExamScore> findAllScore() {
		String hql = "from ExamScore";
		List<ExamScore> list = this.find(hql);
		
		return list;

	}

	

}
